package api

import (
	"api-getaway/basic/config"
	"api-getaway/handler/request"
	"api-getaway/model"
	"github.com/gin-gonic/gin"
	"net/http"
)

func PetList(r *gin.Context) {
	var req request.Pet
	err := r.ShouldBind(&req)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "参数错误",
			"data":    nil,
		})
		return
	}
	var all []model.Pet
	config.DB.Debug().Find(&all)
	r.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "宠物列表展示成功",
		"data":    all,
	})
}

func PetAdd(r *gin.Context) {
	var req request.Pet
	err := r.ShouldBind(&req)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "参数错误",
			"data":    nil,
		})
		return
	}
	pet := model.Pet{
		Name:    req.Name,
		Types:   req.Types,
		Address: req.Address,
		Store:   req.Store,
		Price:   req.Price,
	}
	if err = pet.CreatePet(); err != nil {
		r.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "添加失败",
			"data":    nil,
		})
		return
	}
	r.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "添加成功",
		"data":    pet,
	})
}
